package com.hiwan.system.web.generator;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.org.hiwan.generator.GeneratorFacade;
import cn.org.hiwan.generator.GeneratorProperties;
import cn.org.hiwan.generator.provider.db.table.dict.DictNameTag;
import cn.org.hiwan.generator.provider.db.table.dict.DictOptionsTag;
import cn.org.hiwan.generator.provider.db.table.dict.OrganizationTag;
import cn.org.hiwan.generator.provider.db.table.dict.Tag;
import cn.org.hiwan.generator.provider.db.table.model.ColumnExtend;
import cn.org.hiwan.generator.provider.db.table.model.ColumnExtend.FormType;
import cn.org.hiwan.generator.provider.db.table.model.ColumnExtend.ValidateType;
import cn.org.hiwan.generator.provider.db.table.model.Table;
import cn.org.hiwan.generator.provider.db.table.model.TableExtend;

import com.hiwan.platform.utils.StringUtils;
import com.hiwan.system.pojo.ColumnProperty;
import com.hiwan.system.pojo.TableProperty;


@Controller
@RequestMapping("/generator")
public class GeneratorController {
	
	@RequestMapping("toQuery.do")
	public String toQuery(ModelMap model) throws Exception{
		List<Table> tables = GeneratorFacade.getAllTableSimpleMessage();
		model.put("tables", tables);
		return "generator/toQuery";
	}
	
	@RequestMapping("getColumns.do")
	public String getTableColumn(String tableName,ModelMap model){
		Table table = GeneratorFacade.getTableByName(tableName);
		model.put("table",table);
		return "generator/columnList";
	}
	
	@RequestMapping("definedColumnProperty.do")
	public String definedColumnProperty(ModelMap model,String tableName){
		Table table = GeneratorFacade.getTableByName(tableName);
		
		FormType[] formTypes = ColumnExtend.FormType.values();
		ValidateType[] validateTypes = ColumnExtend.ValidateType.values();
		
		/*List<String> vts = new ArrayList<String>();
		List<String> fts = new ArrayList<String>();
		for (ValidateType vt : validateTypes) {
			vts.add(vt.toString());
		}
		
		for (FormType ft : formTypes) {
			fts.add(ft.toString());
		}*/
		
		
		
		List<String> tagTypes = new ArrayList<String>();
		tagTypes.add("dictOptions");
		//tagTypes.add("dictOptionsWithList");
		tagTypes.add("dictName");
		tagTypes.add("orgName");
		tagTypes.add("userName");
		tagTypes.add("userNameWithoutId");
		tagTypes.add("deptName");
		//tagTypes.add("paramValue");
		model.put("columns",table.getColumns());
		model.put("formTypes",formTypes);
		model.put("validateTypes",validateTypes);
		model.put("tagTypes",tagTypes);
		return "generator/columnProperty";
	}
	
	@RequestMapping("generateCode.do")
	@ResponseBody
	public String generateCode(TableProperty tableProperty,ColumnProperty columnProperty) throws Exception{
		GeneratorFacade g = new GeneratorFacade();
		TableExtend.Builder builder = new TableExtend.Builder(tableProperty.getClassName());
		if(StringUtils.equals("checkbox", tableProperty.getSelectBox())){
			builder.checkbox(true);
		}else if(StringUtils.equals("radio", tableProperty.getSelectBox())){
			builder.radio(true);
		}
		if(StringUtils.equals("on", tableProperty.getActionBtn())){
			builder.actionBtn(true);
		}
		
		List<ColumnExtend> list = new ArrayList<ColumnExtend>();
		
		if(StringUtils.isNotBlank(columnProperty.getColumnName())){
			String[] columnNames = columnProperty.getColumnName().split(",",-1);
			String[] showNames = columnProperty.getShowName().split(",",-1);
			String[] formTypes = columnProperty.getFormType().split(",",-1);
			String[] validateTypes = columnProperty.getValidateType().split(",",-1);
			String[] tagTypes = columnProperty.getTagType().split(",",-1);
			String[] dictTypes = columnProperty.getDictType()==null?new String[0]:columnProperty.getDictType().split(",",-1);
			ColumnExtend ce = null;
			ColumnExtend.Builder ecBuilder = null;
			
			for (int i = 0; i < columnNames.length; i++) {
				ecBuilder = new ColumnExtend.Builder(columnNames[i], StringUtils.isNotBlank(showNames[i])?showNames[i]:columnNames[i]);
				if(validateTypes.length > i && StringUtils.isNotBlank(validateTypes[i])){
					ecBuilder.validateTypes(ValidateType.valueOf(validateTypes[i]));
				}
				if(formTypes.length > i && StringUtils.isNotBlank(formTypes[i])){
					ecBuilder.formType(FormType.valueOf(formTypes[i]));
				}
				if(tagTypes.length > i && StringUtils.isNotBlank(tagTypes[i])){
					ecBuilder.tag(getTag(tagTypes[i],dictTypes[i]));
				}
				ce = ecBuilder.build();
				list.add(ce);
			}
			builder.columnExtends(list.toArray(new ColumnExtend[0]));
		}
		TableExtend te = builder.build();
		g.deleteOutRootDir();
		g.generateByTable(tableProperty.getTableName(), "template",te);
		if(StringUtils.indexOfIgnoreCase(System.getProperties().getProperty("os.name"), "window") != -1){
			Runtime.getRuntime().exec("cmd.exe /c start " + GeneratorProperties.getRequiredProperty("outRoot"));
		}
		return "output path:"+GeneratorProperties.getRequiredProperty("outRoot");
	}
	
	
	private Tag getTag(String tagType,String dictCode){
		
		if(StringUtils.equals("dictOptions", tagType)){
			return new DictOptionsTag(dictCode);
		}else if(StringUtils.equals("dictName", tagType)){
			return new DictNameTag(dictCode);
		}/*else if(StringUtil.isEqualObj("dictOptionsWithList", tagType)){
			return new DictOptionsTag("");
		}*/else if(StringUtils.equals("orgName", tagType)){
			return OrganizationTag.ORGNAME;
		}else if(StringUtils.equals("deptName", tagType)){
			return OrganizationTag.DEPTNAME;
		}else if(StringUtils.equals("userName", tagType)){
			return OrganizationTag.USERNAME;
		}else if(StringUtils.equals("userNameWithoutId", tagType)){
			return OrganizationTag.USERNAMEWITHOUTID;
		}/*else if(StringUtil.isEqualObj("paramValue", tagType)){
			return new ParamValueTag("","");
		}*/else{
			throw new IllegalArgumentException("不存在类型为【"+tagType+"】的标签");
		}
	}
	
}
